// 插件引入
const gulp = require('gulp');
const gutil = require('gutil');
const webpack = require('webpack');
const path = require('path');

// 进行编译
gulp.task('build', function() {
    webpack({
        entry: './src/main.js',
        output: {
            path: path.resolve(__dirname, './dist'),
            publicPath: '/dist/',
            filename: 'build.js'
        },
        module: {
            rules: [
                {
                    test: /\.vue$/,
                    loader: 'vue-loader'
                },
                {
                    test: /\.js$/,
                    loader: 'babel-loader',
                    exclude: /node_modules/
                },
                {
                    test: /\.css$/,
                    loader: "style-loader!css-loader"
                },
                {
                    test: /\.(eot|woff|woff2|ttf)([\?]?.*)$/,
                    loader: "file-loader"
                },
                {
                    test: /\.(png|jpg|gif|svg)$/,
                    loader: 'file-loader',
                    query: {
                        name: '[name].[ext]?[hash]'
                    }
                }
            ]
        },
        resolve: {
            alias: {
                'vue': 'vue/dist/vue.js'
            }
        }
    }, function(err, stats) {
        if(err) throw new gutil.PluginError("webpack", err);
        gutil.log("[webpack]", stats.toString({}));
    });
});

gulp.task('watch', function() {
    gulp.watch('src/**', ['build']);
});